﻿using System;
using System.Drawing;
using BasicFramework.Components;
using BasicFramework.Devices.DirectX9;
using BasicFramework.Rendering.Buffer.DirectX9;
using BasicFramework.Rendering.Primitives.Indexes;
using BasicFramework.Rendering.Primitives.Vertizes;
using BasicFramework.Services.Graphics;
using BasicFrameworkComponentLibrary;
using SlimDX;
using SlimDX.Direct3D9;
using BasicFramework.Services;


namespace Demo_App.Components
{
    [ComponentDependency(typeof(Transform))]
    public class Cube : Component
    {
        IGraphicsService<DX9DeviceContext> graphicsService;

        private Transform trans;
        private DX9DynamicVertexBuffer<ColorVertex> vBuffer;
        private DX9DynamicIndexBuffer<Index> iBuffer;
        private float size;

        public override void Initialize(bool createDependencies)
        {
            base.Initialize(createDependencies);

            this.graphicsService = ServiceProvider.GetService<IGraphicsService<DX9DeviceContext>>();
           this.size = 1;

            this.trans = this.Owner.Find<Transform>();

            Random zufall = new Random();
            int number = zufall.Next(-5, 5);

            trans.Position = new Vector3(trans.Position.X + number, trans.Position.Y + 8, trans.Position.Z);

        }

        public override void Start()
        {
            base.Start();
        }

        public override void Update(float frame)
        {
            base.Update(frame);

            trans.Position = new Vector3(trans.Position.X, trans.Position.Y - 0.1F, trans.Position.Z);
        }

        public override void Draw(float frame)
        {
            base.Draw(frame);

            vBuffer.SetBuffer(0);
            iBuffer.SetBuffer(0);
            this.graphicsService.DeviceContext.Device.SetTransform(TransformState.World, Matrix.Translation(trans.Position));
            this.graphicsService.DeviceContext.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
            this.graphicsService.DeviceContext.Device.SetTransform(TransformState.World, Matrix.Identity);

        }



        public override void LoadResources(bool loadAllContent)
        {
            base.LoadResources(loadAllContent);

            this.vBuffer = new DX9DynamicVertexBuffer<ColorVertex>(GameManager.DeviceContext);
            this.iBuffer = new DX9DynamicIndexBuffer<Index>(GameManager.DeviceContext);

            vBuffer.Add(new ColorVertex(new Vector3(-this.size, -this.size, -this.size), Color.Red.ToArgb()));        // 0
            vBuffer.Add(new ColorVertex(new Vector3(-this.size, this.size, -this.size), Color.Red.ToArgb()));        // 1

            vBuffer.Add(new ColorVertex(new Vector3(this.size, this.size, -this.size), Color.Yellow.ToArgb()));    // 2
            vBuffer.Add(new ColorVertex(new Vector3(this.size, -this.size, -this.size), Color.Yellow.ToArgb()));    // 3 

            vBuffer.Add(new ColorVertex(new Vector3(-this.size, -this.size, this.size), Color.Green.ToArgb()));    // 4
            vBuffer.Add(new ColorVertex(new Vector3(this.size, -this.size, this.size), Color.Green.ToArgb()));    // 5

            vBuffer.Add(new ColorVertex(new Vector3(this.size, this.size, this.size), Color.Blue.ToArgb()));      // 6
            vBuffer.Add(new ColorVertex(new Vector3(-this.size, this.size, this.size), Color.Blue.ToArgb()));      // 7

            vBuffer.Commit();

            iBuffer = new DX9DynamicIndexBuffer<Index>(GameManager.DeviceContext);

            // Front
            iBuffer.Add(new Index(0));
            iBuffer.Add(new Index(1));
            iBuffer.Add(new Index(2));

            iBuffer.Add(new Index(2));
            iBuffer.Add(new Index(3));
            iBuffer.Add(new Index(0));
            // Back
            iBuffer.Add(new Index(4));
            iBuffer.Add(new Index(5));
            iBuffer.Add(new Index(6));

            iBuffer.Add(new Index(6));
            iBuffer.Add(new Index(7));
            iBuffer.Add(new Index(4));
            // Bot
            iBuffer.Add(new Index(0));
            iBuffer.Add(new Index(3));
            iBuffer.Add(new Index(5));

            iBuffer.Add(new Index(5));
            iBuffer.Add(new Index(4));
            iBuffer.Add(new Index(0));
            // Right
            iBuffer.Add(new Index(3));
            iBuffer.Add(new Index(2));
            iBuffer.Add(new Index(6));

            iBuffer.Add(new Index(6));
            iBuffer.Add(new Index(5));
            iBuffer.Add(new Index(3));
            // Top
            iBuffer.Add(new Index(2));
            iBuffer.Add(new Index(1));
            iBuffer.Add(new Index(7));

            iBuffer.Add(new Index(7));
            iBuffer.Add(new Index(6));
            iBuffer.Add(new Index(2));
            // Lefts
            iBuffer.Add(new Index(1));
            iBuffer.Add(new Index(0));
            iBuffer.Add(new Index(4));

            iBuffer.Add(new Index(4));
            iBuffer.Add(new Index(7));
            iBuffer.Add(new Index(1));

            iBuffer.Commit();
        }

        public override void UnloadResources(bool unloadAllContent)
        {
            base.UnloadResources(unloadAllContent);

            this.Dispose(true);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (this.vBuffer != null)
            {
                this.vBuffer.Dispose();
            }
            if (iBuffer != null)
            {
                this.iBuffer.Dispose();
            }
        }






    }
}
